package com.bc.datasource;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;

import com.bc.datasource.vo.GameDetails;
import com.bc.datasource.vo.SendObject;
import com.bc.util.ConfigLoader;
import com.bc.util.FetchAsiaUtil;

public class LiveUtils {

	
	static Log log = LogFactory.getLog(LiveUtils.class); 
	private static final String SERVER_URL = "server_url";
	public static Properties p = null;
	
	static {
		try {
			initConfig();
		} catch (Exception e) {
		}
	}
	public static void initConfig() throws Exception{
		if(p==null){
			p = getConfig("config.properties");
		}
		
	}
	
	
	public static String getGameDetails() throws IOException{
		String target = calJsUrl("/vbsxml/detail.js?r=007");
		
		String details = new String(getServerByte(target),"GBK");
		
		//System.out.println(details);
		return details;
	}
	
	public static List<SendObject> getSos() throws Exception{
		try{
			List<SendObject> sos = initSoList();
			String a = getSbOddsData();
			setSbOddsData(sos, a);
			String detail = getGameDetails();
			//setDetails(sos,detail);
			//System.out.println("扫描亚盘需要较长时间");
			//setYa(sos);
			for(SendObject s:sos){
				if(s.getFinalPan()!=null && s.getFinalPan().length()>0)
					System.out.println(s.toString());
			}
			return sos;
		}catch (Exception e) {
			throw e;
		}
	}
	
	private static void setYa(List<SendObject> sos) {
		for(SendObject s:sos){
			FetchAsiaUtil.setYa(s);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}


	public static void main(String[] args) throws Exception {
		getSos();
	}
	
	
	private static List<GameDetails> converVo(String detail) {
		List<GameDetails> gs = new ArrayList<GameDetails>();
		if(detail.contains("tc[")){
			String[] des = detail.split("tc\\[");
			
			for(String d : des){
				//System.out.println(">>>>"+d);
				if(d.contains("rq")){
					continue;
				}
				d = d.replaceAll("\"", "");
				String[] ts = d.split("\\^");
				String gid = ts[0];
				GameDetails gd = new GameDetails();
				gd.setGameId(gid.split("=")[1]);
				String tts[] = ts[1].split(";");
				for(String dd: tts){

					if(dd.split(",")[0].equals("14")){
						gd.setPocessHome(dd.split(",")[1]);
						gd.setPocessGuest(dd.split(",")[2]);
						continue;
					}
					
					
					if(dd.split(",")[0].equals("43")){
						gd.setActackHome(dd.split(",")[1]);
						gd.setActackGuest(dd.split(",")[2]);
						continue;
					}
					
					if(dd.split(",")[0].equals("43")){
						gd.setActackHome(dd.split(",")[1]);
						gd.setActackGuest(dd.split(",")[2]);
						continue;
					}
					if(dd.split(",")[0].equals("44")){
						gd.setDengerousHome(dd.split(",")[1]);
						gd.setDengerousGuest(dd.split(",")[2]);
						continue;
					}
					if(dd.split(",")[0].equals("13")){
						gd.setRedCardHome(dd.split(",")[1]);
						gd.setRedCardGuest(dd.split(",")[2]);
						continue;
					}
					if(dd.split(",")[0].equals("11")){
						gd.setYellowCardHome(dd.split(",")[1]);
						gd.setYellowCardGuest(dd.split(",")[2]);
						continue;
					}
					if(dd.split(",")[0].equals("3")){
						gd.setShotHome(dd.split(",")[1]);
						gd.setShotGuest(dd.split(",")[2]);
						continue;
					}
					
					if(dd.split(",")[0].equals("4")){
						gd.setShotHomeI(dd.split(",")[1]);
						gd.setShotGuestI(dd.split(",")[2]);
						continue;
					}
					if(dd.split(",")[0].equals("6")){
						gd.setConerHome(dd.split(",")[1]);
						gd.setConerGuest(dd.split(",")[2]);
						continue;
					}
				
				}//end of for
				gs.add(gd);
			}
		}
		return gs;
	}
	
	private static void setDetails(List<SendObject> sos, String detail) {
		List<GameDetails> gs = converVo(detail);
		for(GameDetails gd: gs){
			for(SendObject so : sos){
				if(gd.getGameId().equals(so.getId())){
					//so.setGd(gd);
				}
			}
		}
	}
	
	
	private static void setSbOddsData(List<SendObject> sos, String a) {
		String[] aa = a.split("sData\\[");
		for (Iterator<SendObject> iterator = sos.iterator(); iterator.hasNext();) {
			SendObject so = iterator.next();
			for (int i = 0; i < aa.length; i++) {
				String s = aa[i];
				if(!s.contains("]")){
					continue;
				}
				String[] aaa = s.split("\\]");
				String ss = aaa[1].replace(",[", "").replace("=[[", "");
				if(aaa[0].replace(",[", "").replace("=[[", "").equals(so.getId())){
					try {
						String[] ds = ss.split(",");
						setPan(so, ds);
					} catch (Exception e) {
						
					}
				}
			}
		}
	}
	
	private static void setPan(SendObject so, String[] ds) {
		so.setOriginPan(ds[1]);
		so.setFirstPan(ds[1]);
		so.setFirstPanHome(ds[0]);
		so.setFirstPanGuest(ds[2]);
		
		so.setFinalPan(ds[4]);
		so.setFinalPanHome(ds[3]);
		so.setFinalPanGuest(ds[5]);
		
		so.setLivePan(ds[7]);
		so.setLivePanHome(ds[6]);
		so.setLivePanGuest(ds[8]);
	}
	
	private static String getSbOddsData() throws IOException{
		String url = calJsUrl(p.getProperty("gun_target"));
		byte[] data = getServerByte(url);
		String jsFile = new String(data,"UTF-8");
		System.out.println(jsFile);
		return jsFile;
	}
	
	
	public static Properties getConfig(String url) throws Exception{
		Properties pro = new Properties();
		InputStream in = ConfigLoader.loadConfig(url);
		pro.load(in);
		return pro;
	}
	
	private static List<SendObject> initSoList() throws Exception {
		List<SendObject> sos = new ArrayList<SendObject>();
		String[] gameArrays = getArrays();
		for (int i = 0; i < gameArrays.length; i++) {
			try {
				String s = gameArrays[i];
				if(s.length()>1){
					SendObject so = new SendObject();
					ObjectUtil.setObject(s, so);
					sos.add(so);
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sos;
	}
	
	public static String[] getArrays() throws Exception {
		String jsFile = getGameJs();
		String games1 = jsFile.substring(jsFile.indexOf("A[1]"),jsFile.indexOf("B[1]"));
		games1 = games1.replaceAll(".split\\(\\'\\^\\'\\)", "|");
		String[] gameArrays = games1.split("A\\[");
		return gameArrays;
	}
	
	public static String getGameJs() throws Exception{
		String url = calJsUrl();
		byte[] data = getServerByte(url);
		String jsFile = new String(data,"GBK");
		//log.debug("获取到的js数据：\t"+jsFile);
		return jsFile;
	}
	
	private static byte[] getServerByte(String url) throws IOException {
		byte[] data = Jsoup.connect(url).
				ignoreContentType(true)
				.header("Referer", p.getProperty("refer_url"))
				.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 QQBrowser/3.9.3952.400")
				.timeout(Integer.parseInt(p.getProperty("time_out")))       
				.execute().bodyAsBytes();
		return data;
	}
	
	/**
	 * 获取js data的url路径
	 * @return
	 */
	public static String calJsUrl(){
		return calJsUrl("/vbsxml/bfdata.js?r=007");
	}
	
	/**
	 * 获取js data的url路径
	 * @return
	 */
	public static String calJsUrl(String target){
		String url = p.getProperty(SERVER_URL)+target;
		Date date = new Date();
		long time = date.getTime();
		url += ((time/1000)*1000);
		return url;
	}

	
}
